package com.ethan.lotgateway.netty;

import com.ethan.lotgateway.netty.handler.ChannelManagerHandler;
import com.ethan.lotgateway.netty.handler.MqHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ChannelInitHandler extends ChannelInitializer<SocketChannel> {
    @Autowired MqHandler mqHandler;
    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline pipeline = socketChannel.pipeline();
        pipeline.addLast("logging", new LoggingHandler(LogLevel.INFO));
        //pipeline.addLast(new LengthFieldBasedFrameDecoder(10240, 2, 2, -4, 4, true));
        pipeline.addLast(new ChannelManagerHandler());
        pipeline.addLast(mqHandler);
    }
}
